﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Telerik.Windows.Data;
using IndustrialSmokeCalc.SmokeCalcWCF;

namespace IndustrialSmokeCalc
{
    public partial class Draft : PhoneApplicationPage
    {
        private MeasurementDBDataContext _measurementDBDataContext;
        private ObservableCollection<MeasurementDB> listMeasurementDb; 

        public Draft()
        {
            InitializeComponent();
            listMeasurementDb = new ObservableCollection<MeasurementDB>();
            _measurementDBDataContext = new MeasurementDBDataContext(MeasurementDBDataContext.DBConnectionString);
            LoadMeasurement();
        }

        private void LoadMeasurement()
        {
            var measurementDBs = from MeasurementDB m in _measurementDBDataContext.MeasurementTable select m;
            foreach (var measurement in measurementDBs)
            {
                listMeasurementDb.Add(measurement);
            }

            if (listMeasurementDb.Count > 0)
            {
                listDraft.ItemsSource = listMeasurementDb;
                btnUpload.IsEnabled = true;
            }
            else
            {
                txtEmpty.Visibility = Visibility.Visible;
            }
        }

        private void UploadMeasurement(MeasurementDB m)
        {
            SmokeCalcClient client = new SmokeCalcClient();
            client.AddMeasurementCompleted += AddMeasurementCompleted;
            client.AddMeasurementAsync(m.ChimneyId, m.UserId, m.MeasurementObserver, m.MeasurementDate, m.MeasurementTime1,
                m.MeasurementTime2, m.MeasurementResultRingelmann, m.MeasurementUnitAddr,
                m.MeasurementEmissionAddr, m.MeasurementHeight, m.MeasurementRelativeHeight, m.MeasurementDistance,
                m.MeasurementDir1, m.MeasurementDir2, m.MeasurementEmissionDes1, m.MeasurementEmissionDes2,
                m.MeasurementEmissionColor1, m.MeasurementEmissionColor2, m.MeasurementPoint1, m.MeasurementPoint2,
                m.MeasurementBgDes1, m.MeasurementBgDes2, m.MeasurementBgColor1, m.MeasurementBgColor2, m.MeasurementSky1,
                m.MeasurementSky2, m.MeasurementWind1, m.MeasurementWind2, m.MeasurementWindDir1, m.MeasurementWindDir2,
                m.MeasurementTemp1, m.MeasurementTemp2, m.MeasurementWetTemp, m.MeasurementRh, m.MeasuremenResulttDetail);
        }

        private void AddMeasurementCompleted(object sender, AddMeasurementCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBox.Show("Tidak dapat melakukan koneksi ke server.\r\nCoba periksa koneksi internet anda.");
                NavigationService.Navigate(new Uri("/Menu.xaml", UriKind.Relative));
            }
            else if (e.Result == "sukses")
            {
                
            }
            else
            {
                MessageBox.Show(e.Result);
            }
        }

        private void btnUpload_Click(object sender, RoutedEventArgs e)
        {
            var measurementDBs = from MeasurementDB m in _measurementDBDataContext.MeasurementTable select m;
            foreach (var measurement in measurementDBs)
            {
                UploadMeasurement(measurement);
                DeleteDraft(measurement);
            }
            MessageBox.Show("Data formulir pengamatan telah diunggah ke server.");
            NavigationService.Navigate(new Uri("/Menu.xaml", UriKind.Relative));
        }

        private void DeleteDraft(MeasurementDB measurement)
        {
            _measurementDBDataContext.MeasurementTable.DeleteOnSubmit(measurement);
            _measurementDBDataContext.SubmitChanges();
        }
    }
}